package com.banksoft.front.controller;

import com.banksoft.admin.SystemCache;
import com.banksoft.admin.dao.OrderDao;
import com.banksoft.admin.dao.ProductDao;
import com.banksoft.admin.entity.Order;
import com.banksoft.admin.entity.OrderProduct;
import com.banksoft.admin.entity.Product;
import com.banksoft.admin.entity.User;
import com.banksoft.admin.service.BaseService;
import com.banksoft.admin.service.OrderProductService;
import com.banksoft.admin.service.OrderService;
import com.banksoft.admin.service.UserService;
import com.banksoft.common.BaseController;
import com.banksoft.front.bean.OrderBean;
import com.banksoft.front.bean.Page;
import com.banksoft.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vanjoger on 2017/1/10.
 */
@Controller
@RequestMapping("/front/user/order")
public class FrontUserOrderController extends BaseController {

    @Autowired
    OrderService orderService;
    @Autowired
    BaseService baseService;
    @Autowired
    OrderProductService orderProductService;
    @Autowired
    UserService userService;

    @RequestMapping("/list")
    public String list(String isToday,Model model){
        model.addAttribute("isToday",isToday);
        return "front/front_order_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(HttpServletRequest request,int pageNo,int pageSize,String isToday){
        try{
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);

            Page page = this.orderService.queryPage(user.getId(),pageNo,pageSize,isToday);

            return this.FmtResult(true,null,page);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,null,null);
        }
    }

    @RequestMapping("/teamOrder")
    public String teamOrder(String isToday,Model model){
        model.addAttribute("isToday",isToday);
        return "front/front_team_order_list";
    }

    @RequestMapping("/listTeamOrderData")
    @ResponseBody
    public JSONObject listTeamOrderData(HttpServletRequest request,int pageNo,int pageSize,String isToday){
        try{
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);

            Page page = this.orderService.queryTeamOrderPage(user.getId(),isToday);

            return this.FmtResult(true,null,page);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,null,null);
        }
    }


    @RequestMapping("/detail")
    public String detail(String id, Model model){
        try{
            Order order = this.baseService.findById(OrderDao.class,id);

            List<Product> data = new ArrayList<>();
            List<OrderProduct> productList = this.orderProductService.listByOrderId(order.getId());
            if(StringUtil.isNotEmpty(productList)){
                for(OrderProduct op:productList) {
                    Product product = this.baseService.findById(ProductDao.class, op.getProductId());
                    if (product != null) {
                        String images = product.getImages();
                        if (StringUtil.isNotNull(images)) {
                            String[] arrays = images.split("\\|");
                            product.setImages(arrays[0]);
                        }
                        product.setNum(op.getNum());
                        data.add(product);
                    }
                }
            }
            model.addAttribute("order",order);
            model.addAttribute("list",data);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "front/front_order_detail";
    }

    @RequestMapping("/listUserOrder")
    public String listUserOrder(String id,Model model,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);
        if(!StringUtil.isNotNull(id))
            id = user.getId();
        List<User> userlist = this.userService.listByRecommendId(id);
        if(StringUtil.isNotEmpty(userlist)) {
            String ids = "";
            for (User u : userlist) {
                ids += u.getId() + ",";
            }
            if (ids.length() > 0) {
                ids = ids.substring(0, ids.length() - 1);
            }
            String[] arrays = ids.split(",");
            List<Order> orderlist = this.orderService.listByUserIds(arrays);
            model.addAttribute("list",orderlist);
        }

        return "front/team_order_list";
    }

}
